package com.example.myapplication.tack_30.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.tack_29.DetailActivity;
import com.example.myapplication.tack_29.data.PostData;
import com.example.myapplication.tack_30.data.AddressData;
import com.example.myapplication.utils.Common;
import com.google.gson.Gson;

import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.My> {
    private List<AddressData.DataDTO> list;
    private Context context;
    private View view;
    private Click click;
    private Gson gson;


    public AddressAdapter(List<AddressData.DataDTO> list, Context context,Click click) {
        this.list = list;
        this.context = context;
        this.click = click;
        gson = new Gson();
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_32_shou_huo,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        holder.name.setText("(姓名)"+list.get(position).getName());
        holder.address.setText(list.get(position).getAddressDetail());
        holder.phone.setText("电话号："+list.get(position).getPhone());

        holder.line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = gson.toJson(list.get(position));
                click.close(s);
            }
        });


    }
    public interface Click{
        void close(String msg);
    }
    @Override
    public int getItemCount() {
        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private TextView name;
        private TextView address;
        private TextView phone;
        private LinearLayout line;
        public My(@NonNull View itemView) {
            super(itemView);
            name = view.findViewById(R.id.name);
            address = view.findViewById(R.id.address);
            phone = view.findViewById(R.id.phone);
            line = view.findViewById(R.id.line);
        }
    }
}
